import java.util.*;

/**
 * @author LKQ
 * @date 2022/6/26 23:10
 * @description 快慢指针，快指针走到末尾，慢指针走到中间，然后利用慢指针将前面部分链表反转，
 */
public class Solution2 {
    public static void main(String[] args) {

    }
    public boolean isPalindrome(ListNode head) {
        if(head == null || head.next == null) {
            return true;
        }
        ListNode slow = head, fast = head;
        ListNode pre = head, prepre = null;
        while(fast != null && fast.next != null) {
            pre = slow;
            slow = slow.next;
            fast = fast.next.next;
            // 下面两行将前半部分反转到 pre上
            pre.next = prepre;
            prepre = pre;
        }
        if(fast != null) {
            slow = slow.next;
        }
        while(pre != null && slow != null) {
            if(pre.val != slow.val) {
                return false;
            }
            pre = pre.next;
            slow = slow.next;
        }
        return true;
    }
}
